Import mojo
Import agfx

#rem

<resources name="320x480" >
	<images>
		<image name="GAMESPLASH"  filename="bkg_320x480.png" midhandle="False" precache="True" />	
		...
	</images>
	<animations>
		<animation name="btn_start"  filename="btn_start.png" width="96" height="32" frames="3" midhandle="True" precache="True" />
		...
	</animations>
	<sounds>
		<sound filename="lazer" />  with NO EXTENSION !!!!
		...
	</sounds>
	<musics>
		<music filename="lazer" />  with NO EXTENSION !!!!
		...
	</musics>
	<fonts>
		<fonts filename="fixedsys_12" /> with NO EXTENSION !!!!
		...
	</fonts>
	<tilesets>
		<tileset name="TILES" filename="agfx_collision.png" tile_width="32" tile_height="32" tile_margin="0" tile_spacing="0" precache="False"/>
		...
	</tilesets>
</resources>

#end

Class TResource

	Field parser:XMLParser = New XMLParser
	Field doc:XMLDocument
	Field state:Bool = False
	Method New()
		
	End Method
	
	Method Load:Void(resource_file:String)
	
		Self.state = False
	
		Self.doc = Self.parser.ParseString(LoadString("resources/"+resource_file))
		
		Local node:XMLElement
		
		For node = EachIn doc.Root.Children
		
			Select node.Name()
			
				' ---- [ IMAGES ] ----
				Case "images"
					For Local images:XMLElement = EachIn node.Children
					
						Local str_name:String = images.GetAttribute("name")
						Local str_filename:String = images.GetAttribute("filename")
						Local str_midhandle:String = images.GetAttribute("midhandle")
						Local str_precache:String = images.GetAttribute("precache")
						
						Local midhandle:Bool = False
						Local precache:Bool  = False
						
						If str_midhandle.ToUpper="TRUE" Then midhandle = True
						If str_precache.ToUpper="TRUE" Then precache = True
						
						IMAGES.Load(str_filename,str_name,midhandle)
						
						If (precache) Then IMAGES.Find(str_name).PreCache()
						
					Next
					
				' ---- [ ANIMATIONS ] ----
				Case "animations"
					For Local animations:XMLElement = EachIn node.Children	
					
						Local str_name:String = animations.GetAttribute("name")
						Local str_filename:String = animations.GetAttribute("filename")
						Local str_midhandle:String = animations.GetAttribute("midhandle")
						Local str_precache:String = animations.GetAttribute("precache")
						Local width:Int = Int(animations.GetAttribute("width"))
						Local height:Int = Int(animations.GetAttribute("height"))
						Local frames:Int = Int(animations.GetAttribute("frames"))
					
						Local midhandle:Bool = False
						Local precache:Bool  = False
						
						If str_midhandle.ToUpper="TRUE" Then midhandle = True
						If str_precache.ToUpper="TRUE" Then precache = True
						
						
						IMAGES.LoadAnim(str_filename,width,height,frames,null,midhandle)
						If (precache) Then IMAGES.Find(str_name).PreCache()
						
					Next
					
				' ---- [ SOUNDS ] ----
				Case "sounds"
					For Local sounds:XMLElement = EachIn node.Children	
						SOUNDS.Load(sounds.GetAttribute("filename"))
					Next
				
				' ---- [ MUSICS ] ----
				Case "musics"
					For Local musics:XMLElement = EachIn node.Children	
						SOUNDS.Load(musics.GetAttribute("filename"))
					Next
					
				' ---- [ FONTS ] ----
				Case "fonts"
					For Local fonts:XMLElement = EachIn node.Children	
						FONTS.Load(fonts.GetAttribute("filename"))
					Next
				' ---- [ TILESETS ] ----
				Case "tilesets"
					Print("tilesets")
					For Local tileset:XMLElement = EachIn node.Children
						Local str_name:String = tileset.GetAttribute("name")
						Local str_filename:String = tileset.GetAttribute("filename")
						Local str_precache:String = tileset.GetAttribute("precache")						
						Local tile_margin:Int = Int(tileset.GetAttribute("tile_margin"))
						Local tile_spacing:Int = Int(tileset.GetAttribute("tile_spacing"))
						Local tile_width:Int = Int(tileset.GetAttribute("tile_width"))
						Local tile_height:Int = Int(tileset.GetAttribute("tile_height"))
						
						Local precache:Bool  = False
						If str_precache.ToUpper="TRUE" Then precache = True
						
						IMAGES.LoadTileset(str_filename,tile_width,tile_height,tile_margin,tile_spacing,str_name)
						If (precache) Then IMAGES.Find(str_name).PreCache()
					Next
			End Select
		
		
		Next
			
		Self.state = True
		
	End Method
	
	
	Method IsDone:Bool()
		Return Self.state
	End Method
	
	
End Class